
library IEEE;
use IEEE.std_logic_1164.all;
use work.state_pkg.all;


entity main_state_ctrl is
	port (	clk			: in	std_logic;
		reset			: in	std_logic;
		
		continu : in std_logic;
		
		do_cross : in std_logic;
		do_search : in std_logic;

		sensor_l		: in	std_logic;
		sensor_m		: in	std_logic;
		sensor_r		: in	std_logic;
		
		go_search : out std_logic;
		go_track : out std_logic;
		go_cross : out std_logic;
		go_map : out std_logic;
		go_interrupt : out std_logic;
		bin_state : out std_logic_vector(2 downto 0);
		reset_cross_mem : out std_logic;
		
		led_found_line : out std_logic;
		
		
		
		
		
	
		msg : out string(1 downto 1)
	);
end entity main_state_ctrl;

architecture behavioural of main_state_ctrl is
    
          
    signal state, new_state:  main_state_type;
    signal sensor: std_logic_vector(2 downto 0);
	 signal new_found_line, found_line : std_logic;
    
    --signal continu_state : std_logic;

begin
    sensor <= sensor_l & sensor_m & sensor_r;
    led_found_line <= found_line;
	 
	 
    process (clk)
      begin
        if (rising_edge (clk)) then
            if (reset = '1') then
                state <= mapping;
					 found_line <= '0';
            else
                found_line <= new_found_line;
                state <= new_state;
            end if;
        end if;
    end process;
                
    process (state, sensor, continu, do_search, do_cross)
    begin
        case state is 
          when SEARCHING=>
            go_track <= '1';
            go_search <= '0';
            go_cross <= '1';
            go_map <= '1';
				go_interrupt <= '1';
            bin_state <= "010";
            msg <= "S";
            reset_cross_mem <= '0';
            if (continu = '1' and do_search = '1') then
                new_state <= SEARCHING;
					 reset_cross_mem <= '0';
					 new_found_line <= found_line;
            else
                new_state <= MAPPING;
					 reset_cross_mem <= '0';
					 new_found_line <= '1';
            end if;
				
          when TRACKING =>
            go_track <= '0';
            go_search <= '1';
            go_cross <= '1';
            go_map <= '1';
				go_interrupt <= '1';
            bin_state <= "001";
            msg <= "t";
            reset_cross_mem <= '0';
            new_found_line <= found_line;
            if (continu = '1') then 
                new_state <= TRACKING;
            else
					 new_state <= MAPPING;
            end if;
              
          when CROSSING =>
            go_track <= '1';
            go_search <= '1';
            go_cross <= '0';
            go_map <= '1';
				go_interrupt <= '1';
            bin_state <= "011";
            msg <= "C";
            new_found_line <= found_line;
            if (continu = '1' nand do_cross = '1') then
                new_state <= MAPPING;
                reset_cross_mem <= '1';
            else
                new_state <= CROSSING;
                reset_cross_mem <= '0';
            end if;
          when WHITEINTERRUPT =>
            go_track <= '1';
            go_search <= '1';
            go_cross <= '1';
            go_map <= '1';
				go_interrupt <= '0';
            bin_state <= "100";
            msg <= "I";
				reset_cross_mem <= '0';
            new_found_line <= found_line;            
            if (continu = '1') then 
                new_state <= WHITEINTERRUPT;
            else
					 new_state <= MAPPING;
            end if;
				
          when mapping =>
            go_track <= '1';
            go_search <= '1';
            go_cross <= '1';
            go_map <= '0';
				go_interrupt <= '1';
            msg <= "E";
            bin_state <= "000";                
            reset_cross_mem <= '0';
				new_found_line <= found_line;
				if (sensor = "000") then
                    if (do_cross = '1') then
                        new_state <= CROSSING;
                    else
                        new_state <= TRACKING;
                    end if;
                elsif (sensor = "111") then
                    if (do_search = '1') then  
						  --new_state <= searching;
								if (found_line = '1') then 
									new_state <= WHITEINTERRUPT;
								else
									new_state <= SEARCHING;

								end if;
                    else
                        new_state <= TRACKING;
                    end if;
                else 
                    new_state <= TRACKING;
                end if;        
			end case;
    end process;
end architecture behavioural;